package org.springblade.common.mq;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.aliyun.openservices.ons.api.PropertyValueConst;
import com.github.thierrysquirrel.annotation.MessageListener;
import com.github.thierrysquirrel.annotation.RocketListener;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.RescueNoticeCreateDTO;
import org.springblade.modules.admin.dto.RescueRoomPersonDTO;
import org.springblade.modules.admin.dto.video.TicketFeedBackDTO;
import org.springblade.modules.admin.service.IRescueNoticeService;
import org.springblade.modules.admin.service.suger.IEstateTlwScreenService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Description 紧急呼救消息接收
 * @Author ysq
 * @Date 2023/6/19
 **/
@Slf4j
@ConditionalOnProperty(value = "mq.receive.prod.rescue", havingValue = "true")
@RocketListener(groupID = "GID_SUCCOR",messageModel = PropertyValueConst.BROADCASTING)
public class RescueMessageReceive {

	@Resource
	private IRescueNoticeService rescueNoticeService;
	@Resource
	private IEstateTlwScreenService tlwScreenService;


	@MessageListener(topic = "succor_notice", tag = "rescue_task_create || rescue_room_person")
	public void message(String message) {
		Map<String, Object> map = (Map<String, Object>) JSON.parse(message);
		if (Func.equalsSafe(String.valueOf(map.get("tag")), "rescue_task_create")) {
			this.rescueTaskCreate(message);
		} else {
			this.rescueRoomPerson(message);
		}
	}

	/**
	 * 梯联网收到设备端一键救援按钮呼叫，推送到梯无忧
	 * @param message 来自梯联网的消息
	 */
	private void rescueTaskCreate(String message) {
		log.info("紧急呼叫-救援任务工单创建，接收时间:[{}],接受内容:[{}]", DateUtil.now(), message);
		RescueNoticeCreateDTO noticeCreate = JSONUtil.toBean(message, RescueNoticeCreateDTO.class);
		if (Func.isEmpty(noticeCreate)) {
			return;
		}
//      2025-07 梯联网回调节点改为梯无忧审核确认后，根据确认结果返回，未来有需求再开启
//		TicketFeedBackDTO feedBack = new TicketFeedBackDTO();
//		feedBack.setDeviceId(noticeCreate.getDeviceId());
//		feedBack.setRescueId(noticeCreate.getRescueId());
//		feedBack.setIsCreateRescue(Boolean.FALSE);
//		feedBack.setState(1);
//		// 紧急呼救-创建救援工单回调
//		tlwScreenService.ticketFeedback(feedBack);
		try {
			rescueNoticeService.rescueNoticeSave(noticeCreate);
		} catch (Exception e) {
			log.warn("紧急呼叫-创建救援工单:[{}],异常内容:[{}]", DateUtil.now(), e.getMessage());
		}
	}

	/**
	 * 视频通话中，人员加入、退出通知，用房间号ID作为唯一事件关联关系
	 * 人员退出的时候，有这个人员的登录时间和退出时间，登录的时候只有登录时间，没有退出时间
	 * @param message 信息
	 */
	private void rescueRoomPerson(String message) {
		log.info("人员加入/退出通知，接收时间:[{}],接受内容:[{}]", DateUtil.now(), message);
		RescueRoomPersonDTO roomPerson = JSONUtil.toBean(message, RescueRoomPersonDTO.class);
		if (Func.isEmpty(roomPerson)) {
			return;
		}

		try {
			rescueNoticeService.rescueNoticeSessionSave(roomPerson);
		} catch (Exception e) {
			log.error("人员加入/退出通知:[{}],异常内容:[{}]", DateUtil.now(), e.getMessage());
		}
	}

}
